package database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;

import database.dao.PlayerCollectSingerInfoDao;
import database.dao.PlayerHistorySongInfoDao;
import database.pojo.PlayerCollectSingerInfo;
import database.pojo.PlayerHistorySongInfo;

import util.JdbcUtils;
import util.MyFactory;

public class HistoryDBServer {
	public PlayerHistorySongInfo inserPlayerHistorySong(PlayerHistorySongInfo pInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("PlayerHistorySongInfoDao", PlayerHistorySongInfoDao.class).insert(pInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return pInfo;
	}
	
	public PlayerHistorySongInfo updatePlayerHistorySongInfo(PlayerHistorySongInfo pInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("PlayerHistorySongInfoDao", PlayerHistorySongInfoDao.class).update(pInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return pInfo;
	}
	
	public ArrayList<PlayerHistorySongInfo> selectPlayerHistorySongInfoByOpenId(String openId) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("PlayerHistorySongInfoDao", PlayerHistorySongInfoDao.class).selectAllByOpenId(openId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	public boolean isExitPlayerHistorySongInfo(String openId, String songId) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("PlayerHistorySongInfoDao", PlayerHistorySongInfoDao.class).isExist(openId,songId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	public int selectMaxScoreByOpenIdAndSongId(String openId,String songId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("PlayerHistorySongInfoDao", PlayerHistorySongInfoDao.class).selectMaxScoreByOpenIdAndSongId(openId, songId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
}